<template>
    <div class="logincontainer">
        <el-form :model="loginModel" ref="form" :rules="rules" class="loginform" :inline="false" size="large">
            <img style="width: 40px;height: 40px;" src="../../assets/logo.jpg">
            <el-form-item>
                <div class="logintitle">
                    转转乐后台系统
                </div>
            </el-form-item>
            <el-form-item prop="username">
                <el-input v-model="loginModel.username" placeholder="请输入账户"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" v-model="loginModel.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-row :gutter="20">
                    <el-col :span="16" :offset="0">
                        <el-input v-model="loginModel.code" placeholder="请输入验证码"></el-input>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <img :src="imgsrc" @click="getImg" class="image">
                    </el-col>
                </el-row>
            </el-form-item>
            <el-form-item>
                <el-row style="width: 100%;">
                    <el-col style="padding-right: 10px; padding-left: 0px;" :span="12" :offset="0">
                        <el-button type="primary" style="width: 100%;" @click="commit">登录</el-button>
                    </el-col>
                    <el-col style="padding-right: 0px; padding-left: 10px;" :span="12" :offset="0">
                        <el-button type="primary" style="width: 100%;" size="large" @click="">重置</el-button>
                    </el-col>
                </el-row>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { imageApi, lgoinApi } from '@/api/user';
import { FormInstance } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
import { userStore } from '@/store/user';
import { useRouter } from 'vue-router';
const router = useRouter();
const store = userStore();

// 验证码
const imgsrc = ref('')
// 表单ref
const form = ref<FormInstance>();
// 表单绑定对象
const loginModel = reactive({
    username: "",
    password: "",
    code: ""
})

// 获取验证码
const getImg = async () => {
    let res = await imageApi()
    if(res && res.code == 200){
        console.log(res)
        imgsrc.value = res.data;
    }
}
// 登录提交
const commit = () => {
    form.value?.validate(async (valid) => {
        if (valid) {
            let res = await lgoinApi(loginModel);
            store.setUserId(res.data.userId);
            router.push({path: '/dashboard'});
        }
    })
}

onMounted(() => {
    getImg()
})

const rules = reactive({
    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
    code: [{required: true, message: '请填写验证码', trigger: 'blur'}]
})

</script>

<style scoped>
.logincontainer {
  height: 100%;
  background-color: #fff;
  background-image: url("../../assets/login.jpg");
  display: flex;
  justify-content: center;
  align-items: center;

  .loginform {
    height: 360px;
    width: 420px;
    padding: 20px 35px;
    border-radius: 10px;
    background-color: #fff;

    .logintitle {
      display: flex;
      justify-content: center;
      color: #606266;
      width: 100%;
      font-size: 24px;
      font-weight: 600;
    }

    .image {
      height: 40px;
      width: 100%;
      cursor: pointer;
    }
  }
}
</style>